package com.daijt.mobile.persistence;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonManagedReference;

@Entity
@Table(name = "check_list")
public class CheckList implements Serializable {

	private static final long serialVersionUID = -4586609352012373792L;
	
	private long id;
	private String localId;
	private String listName;
	private String listType;
	private long version;
	private String status;
	
	private User owner;
	
	private List<ListItem> items;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column
	public long getId() {
		return id;
	}
	
	public void setId(long id) {
		this.id = id;
	}
	
	@Column
	public String getLocalId() {
		return localId;
	}
	public void setLocalId(String localId) {
		this.localId = localId;
	}
	
	@Column
	public String getListName() {
		return listName;
	}
	public void setListName(String listName) {
		this.listName = listName;
	}
	
	@Column
	public String getListType() {
		return listType;
	}
	public void setListType(String listType) {
		this.listType = listType;
	}
	
	@Column
	public long getVersion() {
		return version;
	}
	public void setVersion(long version) {
		this.version = version;
	}
	
	@Column
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="userId", nullable=false)
	public User getOwner() {
		return owner;
	}
	public void setOwner(User owner) {
		this.owner = owner;
	}
	
	@JsonManagedReference
	@OneToMany(cascade={CascadeType.REMOVE},
			fetch=FetchType.LAZY)
	@JoinColumn(name="listId", referencedColumnName="id", insertable = false, updatable = false)
	public List<ListItem> getItems() {
		return items;
	}
	public void setItems(List<ListItem> items) {
		this.items = items;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("id: ");
		sb.append(id);
		sb.append(" localId: ");
		sb.append(localId);
		sb.append(" listName: ");
		sb.append(listName);
		sb.append(" listType: ");
		sb.append(listType);
		sb.append(" version: ");
		sb.append(version);
		sb.append(" status: ");
		sb.append(status);
		
		sb.append(" owner: ");
		if (owner == null) {
			sb.append("null");
		}
		else {
			sb.append("[");
			sb.append(owner.toString());
			sb.append("]");
		}
		
		sb.append(" items: ");
		if (items == null) {
			sb.append("null");
		}
		else {
			for (ListItem item : items) {
				sb.append("[");
				sb.append(item.toString());
				sb.append("] ");
			}
		}
		
		return sb.toString();
	}
	

}
